package com.feonix.common.datasource;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.logicalcobwebs.proxool.ProxoolDataSource;
import org.logicalcobwebs.proxool.ProxoolException;
import org.logicalcobwebs.proxool.ProxoolFacade;

import com.feonix.common.util.StringUtil;

public class DataSourceManager {

	protected static final Log logger = LogFactory.getLog(DataSourceManager.class);

	private static final Map<String, DataSourceConfig> DATA_SOURCES_CONFIG = new HashMap<String, DataSourceConfig>();
	private static AdaptiveDataSource adaptiveDataSource;
	private static DataSourceConfig defaultDataSourceConfig;

	public static boolean addDataSource(DataSourceConfig dataSourceConfig) {
		boolean isSuccess = false;
		ProxoolDataSource dateSource = dataSourceConfig.getDataSource();
		if (dateSource != null && !DATA_SOURCES_CONFIG.containsKey(dataSourceConfig.getAlias())) {
			DATA_SOURCES_CONFIG.put(dataSourceConfig.getAlias(), dataSourceConfig);
			adaptiveDataSource.add(dataSourceConfig.getAlias(), dateSource);
			isSuccess = true;
		}
		return isSuccess;
	}

	public static boolean updataDataSource(DataSourceConfig dataSourceConfig) {
		boolean bool = false;
		ProxoolDataSource dataSource = dataSourceConfig.getDataSource();
		if (dataSource != null && removeDataSource(dataSourceConfig.getAlias())) {
			bool = addDataSource(dataSourceConfig);
		}

		return bool;
	}

	public static boolean removeDataSource(String alias) {
		boolean isSuccess = false;
		if (StringUtil.isNotEmpty(alias)) {
			DATA_SOURCES_CONFIG.remove(alias);
			adaptiveDataSource.remove(alias);
			try {
				ProxoolFacade.removeConnectionPool(alias);
				isSuccess = true;
			} catch (ProxoolException proxoolException) {
				logger.warn("removeDataSource warn" + alias);
			}
			
		}
		return isSuccess;
	}

	public static void loadDataSource() {
		loadDefaultDataSource();
		loadCustomDataSource();
	}

	public static void loadDefaultDataSource() {
		//获取默认数据库配置
		DataSourceConfig dataSourceConfig = DataSourceConfig.getDefaultDataSourceConfig();
		defaultDataSourceConfig = dataSourceConfig;
		try {
			ProxoolFacade.removeConnectionPool(dataSourceConfig.getAlias());
		} catch (ProxoolException proxoolException) {
			logger.warn(proxoolException.getMessage());
		}
		adaptiveDataSource.setDefault(defaultDataSourceConfig.getDataSource());
	}

	public static void loadCustomDataSource() {
		List<DataSourceConfig> dataSourceConfigs = DataSourceConfig.getCustomDataSourceConfigs();
		if (dataSourceConfigs != null)
			for (DataSourceConfig dataSourceConfig : dataSourceConfigs){
				addDataSource(dataSourceConfig);
			}
	}

	public static void setAdaptiveDataSource(AdaptiveDataSource paramAdaptiveDataSource) {
		if (adaptiveDataSource == null)
			adaptiveDataSource = paramAdaptiveDataSource;
	}

	public static DataSourceConfig getDefaultDataSourceConfig() {
		return defaultDataSourceConfig;
	}

	public static Map<String, DataSourceConfig> getDataSourcesConfig() {
		return DATA_SOURCES_CONFIG;
	}

	public static AdaptiveDataSource getAdaptiveDataSource() {
		return adaptiveDataSource;
	}

}
